package net.xumf.dao.master;

import net.xumf.domain.master.SystemRole;
import net.xumf.domain.master.SystemUser;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;

import java.util.Optional;

public interface SystemUserRepository extends PagingAndSortingRepository<SystemUser, Long> {

    @Query(value = "SELECT su FROM SystemUser su WHERE su.username like %?1%",
            countQuery = "SELECT count(su) FROM SystemUser su WHERE su.username like %?1%")
    Page<SystemUser> findByUsername(String username, Pageable pageable);

    Optional<SystemUser> findByLoginName(String loginName);

    @Query(value = "SELECT su FROM SystemUser su WHERE (su.username like %?1% or su.loginName like %?1% or su.email like %?1% or su.mobile like %?1% )",
            countQuery = "SELECT count(su) FROM SystemUser su WHERE (su.username like %?1% or su.loginName like %?1% or su.email like %?1% or su.mobile like %?1%)")
    Page<SystemUser> findBySearchText(String searchText, Pageable pageable);

    @Query(value = "SELECT su FROM SystemUser su WHERE (su.username like %?1% or su.loginName like %?1% or su.email like %?1% or su.mobile like %?1% ) and su.status = ?2",
            countQuery = "SELECT count(su) FROM SystemUser su WHERE (su.username like %?1% or su.loginName like %?1% or su.email like %?1% or su.mobile like %?1%) and su.status = ?2")
    Page<SystemUser> findBySearchTextAndStatus(String searchText, Integer status, Pageable pageable);

}